/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2018-12-13     balanceTWK    add sdcard port file
 * 2019-06-11     WillianChan   Add SD card hot plug detection
 */

 #include <rtthread.h>
 #include <rtdevice.h>
 #include <dfs_fs.h>
 
 #define DBG_TAG "app.port_sdcard"
 #define DBG_ENABLE 1
 #define DBG_LVL DBG_LOG
 #include <rtdbg.h>
 
 
 static int _sdcard_mount(void)
 {
    rt_device_t device;
    int try=0;
    do{
        device = rt_device_find("sd1p0");
        try++;
        if(try>100){
            LOG_I("Find device sd1p0 failed!\n");
            break;
        }
        rt_thread_mdelay(20);
    } while(device ==RT_NULL);
    rt_kprintf("try times %d\n", try);


#ifdef BSP_USING_FATFS_ROOTFS
    RT_ASSERT(dfs_mount("sd1p0", "/sdcard", "elm", 0, 0) == 0);
    LOG_I("mount to '/sdcard' success!");
#endif

    return 0;
 }
 
 static int _sdcard_unmount(void)
 {
     rt_thread_mdelay(200);
     RT_ASSERT(dfs_unmount("/sdcard") == 0);
     LOG_I("Unmount '/sdcard' success");

     return 0;
 }
 
 INIT_APP_EXPORT(_sdcard_mount); 
 